﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MyEmulators2
{
    class ScraperProvider
    {
        List<Import_Scraper> scrapers = null;
        Importer importer = null; 
        bool importTop = false;
        bool importExact = false;

        public ScraperProvider(Importer importer)
        {
            init();
            Update();
        }

        bool doWork()
        {
            if (importer == null)
                return true;
            else
                return importer.DoWork;
        }

        public void Update()
        {
            importTop = Options.Instance.GetBoolOption("importtop");
            importExact = Options.Instance.GetBoolOption("importexact");
        }

        void init()
        {
            scrapers = new List<Import_Scraper>();

            Import_Scraper mobyGames = new Import_Scraper(Script.MobyGames, importer);
            if (mobyGames.IsReady)
                scrapers.Add(mobyGames);

            Import_Scraper maws = new Import_Scraper(Script.Maws, importer);
            if (maws.IsReady)
                scrapers.Add(maws);
        }

        public void GetMatches(RomMatch romMatch)
        {
            List<Import_Scraper> lScrapers;
            lock (scrapers)
                lScrapers = new List<Import_Scraper>(scrapers);

            //get clean search term
            string searchString = StripRomCodes(romMatch.Title).ToLower();

            List<ScraperResult> results = new List<ScraperResult>();

            foreach (Import_Scraper scraper in lScrapers)
            {
                if (!doWork())
                    return;

                results.AddRange(scraper.GetResults(searchString));
            }

            if (!doWork())
                return;

            //get parent title to try and match platform
            string searchSystem = romMatch.Game.ParentEmulator.Title.ToLower();

            int possibleTitleMatches = 0;
            ScraperResult titleSite = null;

            int roughTitleMatches = 0;
            ScraperResult roughTitleSite = null;

            int possibleSystemMatches = 0;
            ScraperResult systemSite = null;

            int roughSystemMatches = 0;
            ScraperResult roughSystemSite = null;

            bool isExact = false;

            foreach (ScraperResult result in results)
            {
                if (!doWork())
                    return;

                if (result.Title.ToLower() == searchString && result.System.ToLower() == searchSystem)
                {
                    //exact match break and get details
                    isExact = true;
                    possibleTitleMatches = 1;
                    titleSite = result;
                    break;
                }
                else if (result.Title.ToLower() == searchString)
                {
                    //if it's the first title match, set
                    if (possibleTitleMatches == 0)
                        titleSite = result;
                    possibleTitleMatches++;
                }
                else if (result.Title.ToLower().Contains(searchString) || searchString.Contains(result.Title.ToLower()))
                {
                    if (roughTitleMatches == 0)
                        roughTitleSite = result;
                    roughTitleMatches++;
                }
                else if (result.System.ToLower() == searchSystem)
                {
                    //if it's the first platform match, set
                    if (possibleSystemMatches == 0)
                        systemSite = result;
                    possibleSystemMatches++;
                }
                else if (result.System.ToLower().Contains(searchSystem) || searchSystem.Contains(result.System.ToLower()))
                {
                    if (roughSystemMatches == 0)
                        roughSystemSite = result;
                    roughSystemMatches++;
                }
            }

            ScraperResult matchSite = null;
            if (possibleTitleMatches == 1 && (isExact || !importExact))
                matchSite = titleSite;
            else if (possibleSystemMatches == 1 && !importExact)
                matchSite = systemSite;
            else if (results.Count > 0 && importTop)
                matchSite = results[0];

            if (!doWork())
                return;

            if (matchSite != null) //approved match
            {
                romMatch.GameDetails = matchSite;
                romMatch.Approved = true;
            }
            else
            {
                //select best match but don't approve
                romMatch.Approved = false;

                if (titleSite != null)
                    romMatch.GameDetails = titleSite;
                else if (systemSite != null)
                    romMatch.GameDetails = systemSite;
                else if (roughTitleSite != null)
                    romMatch.GameDetails = roughTitleSite;
                else if (roughSystemSite != null)
                    romMatch.GameDetails = roughSystemSite;
                else if (results.Count > 0)
                    romMatch.GameDetails = results[0];
            }
            romMatch.InfoChecked = true;
            romMatch.PossibleGameDetails = results.ToArray();
        }

        string StripRomCodes(string title)
        {
            Regex objRegEx = new Regex(@"\(.*?\)");
            return objRegEx.Replace(title, "");
        }
    }
}
